//
//  HCDiscoverSearchView.swift
//  Hweibome
//
//  Created by apple on 15/12/7.
//  Copyright © 2015年 hechuan. All rights reserved.
//

import UIKit

class HCDiscoverSearchView: UIView ,UITextFieldDelegate{

    @IBOutlet weak var TextFieldRightCon: NSLayoutConstraint!
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var textField: UITextField!
    //bug 可以没有
//    required init?(coder aDecoder: NSCoder) {
//        // super.init(coder :  aDecoder)
//             super.init(coder: aDecoder)
//        
//        fatalError("init(coder:) has not been implemented")
//    }
//    代码创建控件
//    override init (frame:CGRect)
//    {
//        super.init(frame: frame)
//    }
//    
    //加载view
    class func searchView () -> HCDiscoverSearchView
        
    {//HCDiscoverSearchView
        return NSBundle.mainBundle().loadNibNamed("HCDiscoverSearchView", owner: nil, options: nil).last! as! HCDiscoverSearchView
    }
//    override init(nibName : String?, bundle : NSBundle?) {
//        super.init(nibName:"HCDiscoverSearchView", bundle: nil)
//    }



    
    override func awakeFromNib() {
        
        let leftView = UIImageView (image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.frame = CGRectMake(0, 0, frame.height, frame.height)
        
        leftView.contentMode = .Center
        textField.leftView = leftView
        textField.leftViewMode = UITextFieldViewMode.Always
        
    }
    

    
    @IBAction func didClickCancelButton(sender: AnyObject) {
        textField.resignFirstResponder()
        
        self.TextFieldRightCon.constant = 0
        UIView.animateWithDuration(0.3) { () -> Void in
            self.textField.layoutIfNeeded()
        }
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        self.TextFieldRightCon.constant = self.cancelButton.frame.width
        UIView.animateWithDuration(0.3) { () -> Void in
            self.textField.layoutIfNeeded()
        }
    }
}
